<template>
  <div>
    <div class="banner">
      <img class="img" src="@/assets/topic.jpg" alt="话题" />
    </div>
    <div class="main">
      <div class="head">
        <div class="title">{{ title }}</div>
        <van-button class="subscribe" size="small" @click="handleSubscribe"><i class="fa fa-plus"></i> 订阅</van-button>
      </div>
      <div class="body">
        <em>{{ view | formatNumber }}</em>浏览<em>{{ discuss | formatNumber }}</em>讨论
      </div>
    </div>
    <template v-if="hasActiveUsers">
      <div class="foot">
        <div class="list">
          <user-avatar
            v-for="item in partActiveUsers"
            :key="item.user_info.uid"
            :url="item.user_info.face"
            :vip-status="item.user_info.vip.vipStatus"
            :official="item.user_info.official_verify"
            size="sm"
            class="face"
            @click.native="toSpace(item.user_info.uid)"
          />
        </div>
        <span class="more" @click="openPopup">更多活跃用户<i class="fa fa-dropdown"></i></span>
      </div>
      <van-popup
        :show.sync="popupShow"
      >
        <template #title>更多活跃用户</template>
        <ul class="user-list">
          <li
            v-for="item in activeUsers"
            :key="item.user_info.uid"
            @click="toSpace(item.user_info.uid)"
          >
            <user-avatar
              :url="item.user_info.face"
              :vip-status="item.user_info.vip.vipStatus"
              :official="item.user_info.official_verify"
              size="sm"
              class="face"
            />
            <span class="name">{{ item.user_info.uname }}</span>
          </li>
        </ul>
      </van-popup>
    </template>
  </div>
</template>

<script>
import { Button as VanButton } from 'vant'
import UserAvatar from '@/components/user-avatar/UserAvatar.vue'
import VanPopup from '@/components/popup/Popup.vue'
import { formatNumber } from '@/filters/filters'
export default {
  name: 'TopicInfo',
  inject: ['rootRouter'],
  components: {
    VanButton,
    VanPopup,
    UserAvatar
  },
  props: {
    topicId: {
      type: Number
    },
    title: {
      type: String
    },
    view: {
      type: Number,
      default: 0
    },
    discuss: {
      type: Number,
      default: 0
    },
    activeUsers: {
      type: Array
    }
  },
  data () {
    return {
      popupShow: false
    }
  },
  computed: {
    hasActiveUsers () {
      return this.activeUsers.length
    },
    partActiveUsers () {
      return this.activeUsers.slice(0, 5)
    }
  },
  filters: {
    formatNumber
  },
  methods: {
    handleSubscribe () {
      // TODO: 处理订阅逻辑
    },
    openPopup () {
      this.popupShow = true
    },
    toSpace (mid) {
      this.rootRouter.push({ name: 'space', params: { mid } })
    }
  }
}
</script>
<style lang="scss" scoped>
  .banner{
    position: relative;
    padding-top: 176/750*100%;
    overflow: hidden;
    .img{
      position: absolute;
      left: 0;
      top: 0;
      display: block;
      width: 100%;
      height: 100%;
      object-fit: cover;
      object-position: 50% 50%;
    }
  }
  .main{
    display: flex;
    flex-direction: column;
    padding: rem(12px) rem(16px);
    .head{
      display: flex;
      .title{
        flex: 1;
        font-size: rem(16px);
      }
      .subscribe{
        color: $theme-color;
      }
    }
    .body{
      font-size: rem(14px);
      color: $color-text-secondary;
      em{
        margin-right: rem(5px);
        font-style: normal;
        color: $color-text-primary;
        ~em{
          margin-left: rem(15px);
        }
      }
    }
  }
  .foot{
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: rem(10px) rem(12px);
    @include border1px(#e7e7e7, 1px 0 0 0);
    .list{
      display: flex;
      .face{
        margin-right: rem(5px);
      }
    }
    .more{
      font-size: rem(12px);
      color: $color-text-secondary;
    }
  }
  .user-list {
    li {
      display: flex;
      align-items: center;
      padding: rem(10px);
      font-size: rem(14px);
      color: $color-text-regular;
      .face{
        margin-right: rem(10px);
      }
    }
  }
</style>
